package com.qingmu.club.minaapi.controller.club;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qingmu.club.minaapi.custom.logic.UsersWxLogic;
import com.qingmu.club.minaapi.custom.service.CustomClubStaffScoreService;
import com.qingmu.club.minaapi.enums.DateTypeEnum;
import com.qingmu.club.minaapi.enums.LearnTypeEnum;
import com.qingmu.club.minaapi.plus.entity.Award;
import com.qingmu.club.minaapi.plus.entity.ClubStaff;
import com.qingmu.club.minaapi.plus.entity.ClubStaffScore;
import com.qingmu.club.minaapi.plus.service.AwardService;
import com.qingmu.club.minaapi.utils.DateUtil;
import com.qingmu.club.minaapi.utils.ResponseObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 * 学习排行 前端控制器
 * </p>
 *
 * @author duke
 * @since 2019-08-20
 */
@RestController
@CrossOrigin(origins = "*", methods = {RequestMethod.POST, RequestMethod.GET})
@Api(value = "学习排行(俱乐部端小程序)", description = "学习排行(俱乐部端小程序)")
@RequestMapping("/club/score")
public class ClubStaffScoreController {

    @Autowired
    CustomClubStaffScoreService customClubStaffScoreService;

    @Autowired
    AwardService awardService;

    @Autowired
    UsersWxLogic usersWxLogic;

    @RequestMapping(value = "/getAward", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    @ResponseBody
    @ApiOperation(value = "查看奖励说明列表")
    public ResponseEntity getAward(
            @ApiParam(required = true, value = "人员id") @RequestParam(required = true) String staffId,
            @ApiParam(required = true, value = "门店id") @RequestParam(required = true) String shopId
    ) {
        ClubStaff clubStaff =this.usersWxLogic.getClubStaff(staffId);
        if(clubStaff==null){
            return ResponseObject.error(401,"人员信息找不到");
        }
        QueryWrapper qw = new QueryWrapper();
        qw.eq(Award.SHOP_ID,shopId);
        qw.eq(Award.TYPE,0);
        Award shop = this.awardService.getOne(qw);

        QueryWrapper qw2 = new QueryWrapper();
        qw2.eq(Award.CLUB_ID,clubStaff.getClubId());
        qw2.eq(Award.TYPE,1);
        Award club = this.awardService.getOne(qw2);

        Map<String,Object> map = new HashMap<>();
        map.put("club",club);
        map.put("shop",shop);
        map.put("default","奖励内容");
        return ResponseObject.success(map);
    }

    @RequestMapping(value = "/addAward", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ResponseBody
    @ApiOperation(value = "添加奖励说明")
    public ResponseEntity addAward(
            @ApiParam(required = true, value = "人员id") @RequestParam(required = true) String staffId,
            @ApiParam(required = true, value = "奖励说明内容") @RequestParam(required = true) String content,
            @ApiParam(required = true, value = "人员身份") @RequestParam(required = true) String staffTag,
            @ApiParam(required = false, value = "门店id") @RequestParam(required = false) String shopId
    ) {
        ClubStaff clubStaff =this.usersWxLogic.getClubStaff(staffId);
        if(clubStaff==null){
            return ResponseObject.error(401,"人员信息找不到");
        }
        //  * 人员角色0-教练1-课程顾问2-店主3-管理员
        if(staffTag.equals("isCoach") || staffTag.equals("isCounselor")){
            return ResponseObject.error(556,"无权限");
        }
        Integer type = 0 ;
        if(staffTag.equals("isManager")){
            type=1;
        }

        if(type.equals(1)){
            QueryWrapper<Award> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq(Award.CLUB_ID,clubStaff.getClubId());
            List<Award> awardList = this.awardService.list(queryWrapper);
            if(!CollectionUtils.isEmpty(awardList)){
                this.awardService.remove(queryWrapper);
            }
        }else {
            QueryWrapper<Award> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq(Award.SHOP_ID,shopId);
            List<Award> awardList = this.awardService.list(queryWrapper);
            if(!CollectionUtils.isEmpty(awardList)){
                this.awardService.remove(queryWrapper);
            }
        }

        Award award = new Award();
        award.setStaffId(staffId);
        award.setContent(content);
        award.setType(type);
        award.setStatus(1);
        award.setClubId(clubStaff.getClubId());
        if(!StringUtils.isEmpty(shopId))award.setShopId(shopId);
        boolean state = this.awardService.save(award);
        if(!state){
            return ResponseObject.error();
        }

        return ResponseObject.success();
    }

    @RequestMapping(value = "/removeAward", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ResponseBody
    @ApiOperation(value = "删除奖励说明")
    public ResponseEntity removeAward(
            @ApiParam(required = true, value = "人员id") @RequestParam(required = true) String staffId,
            @ApiParam(required = true, value = "奖励说明id") @RequestParam(required = true) String id
    ) {
        ClubStaff clubStaff =this.usersWxLogic.getClubStaff(staffId);
        if(clubStaff==null){
            return ResponseObject.error(401,"人员信息找不到");
        }
        boolean state = this.awardService.removeById(id);
        if(!state){
            return ResponseObject.error();
        }

        return ResponseObject.success();
    }

    @RequestMapping(value = "/updateAward", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ResponseBody
    @ApiOperation(value = "修改奖励说明")
    public ResponseEntity updateAward(
            @ApiParam(required = true, value = "人员id") @RequestParam(required = true) String staffId,
            Award award
    ) {
        ClubStaff clubStaff =this.usersWxLogic.getClubStaff(staffId);
        if(clubStaff==null){
            return ResponseObject.error(401,"人员信息找不到");
        }
        boolean state = this.awardService.updateById(award);
        if(!state){
            return ResponseObject.error();
        }

        return ResponseObject.success();
    }

    @RequestMapping(value = "/queryAwardByStaffId", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ResponseBody
    @ApiOperation(value = "查看人员奖励说明")
    public ResponseEntity queryAwardByStaffId(
            @ApiParam(required = true, value = "人员id") @RequestParam(required = true) String staffId
    ) {
        ClubStaff clubStaff =this.usersWxLogic.getClubStaff(staffId);
        if(clubStaff==null){
            return ResponseObject.error(401,"人员信息找不到");
        }

        QueryWrapper<Award> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(Award.STAFF_ID,staffId);
        List<Award> awards = this.awardService.list(queryWrapper);

        return ResponseObject.success(awards);
    }


    @RequestMapping(value = "/rankingList", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ResponseBody
    @ApiOperation(value = "学习排行")
    public ResponseEntity rankingList(
            @ApiParam(required = true, value = "人员id") @RequestParam(required = true) String staffId,
            @ApiParam(required = true, value = "俱乐部id") @RequestParam(required = true) String clubId,
            @ApiParam(required = true, value = "SCORE学习成绩 HOURS学习工时") @RequestParam(required = true) LearnTypeEnum learnType,
            @ApiParam(required = true, value = "统计纬度") @RequestParam(required = true) DateTypeEnum dateType
    ) {

        ClubStaff clubStaff =this.usersWxLogic.getClubStaff(staffId);
        if(clubStaff==null){
            return ResponseObject.error(401,"人员信息找不到");
        }
        Date date = new Date(); //当前时间
        String year= new SimpleDateFormat("yyyy").format(date);//年
        String month= new SimpleDateFormat("yyyy-MM").format(date);//月
        HashMap<String,Object> week = DateUtil.getFristAndLashOfWeekDay(date);//周
        HashMap<String,Object> quarter = DateUtil.getFristAndLashOfDuarterDay(date);//季度

        Map<String, Object> params = new HashMap<>();
        params.put(ClubStaffScore.CLUB_ID, clubId);

        List<Map<String,Object>> mapList = new ArrayList<>();

        if(learnType.equals(LearnTypeEnum.SCORE)) {//**************************学习成绩统计****************************
            if (dateType.equals(DateTypeEnum.YEAR)) { //年排行

                params.put(ClubStaffScore.CREATE_TIME, year);
                mapList = customClubStaffScoreService.yearScoreRankingList(params);

            } else if (dateType.equals(DateTypeEnum.QUARTER)) {//季度排行

                params.put("startDate", quarter.get("startDate"));
                params.put("endDate", quarter.get("endDate"));
                mapList = customClubStaffScoreService.quarterScoreRankingList(params);

            } else if (dateType.equals(DateTypeEnum.MONTH)) {//月排行

                params.put(ClubStaffScore.CREATE_TIME, month);
                mapList = customClubStaffScoreService.monthScoreRankingList(params);

            } else if (dateType.equals(DateTypeEnum.WEEK)) {//周排行

                params.put("startDate", week.get("startDate"));
                params.put("endDate", week.get("endDate"));
                mapList = customClubStaffScoreService.weekScoreRankingList(params);

            } else {

                mapList = customClubStaffScoreService.sumScoreRankingList(params); //总排行

            }
        }else {//**************************学习工时统计****************************
            if (dateType.equals(DateTypeEnum.YEAR)) { //年排行

                params.put(ClubStaffScore.CREATE_TIME, year);
                mapList = customClubStaffScoreService.yearHoursRankingList(params);

            } else if (dateType.equals(DateTypeEnum.QUARTER)) {//季度排行

                params.put("startDate", quarter.get("startDate"));
                params.put("endDate", quarter.get("endDate"));
                mapList = customClubStaffScoreService.quarterHoursRankingList(params);

            } else if (dateType.equals(DateTypeEnum.MONTH)) {//月排行

                params.put(ClubStaffScore.CREATE_TIME, month);
                mapList = customClubStaffScoreService.monthHoursRankingList(params);

            } else if (dateType.equals(DateTypeEnum.WEEK)) {//周排行

                params.put("startDate", week.get("startDate"));
                params.put("endDate", week.get("endDate"));
                mapList = customClubStaffScoreService.weekHoursRankingList(params);

            } else {

                mapList = customClubStaffScoreService.sumHoursRankingList(params); //总排行

            }
        }
        return ResponseObject.success(mapList);
    }


}

